import util from '@/libs/util';
import CONSTANTS from '@/config/constants';
import getDate from '@/util/getDate.js';
import Cookies from 'js-cookie';

export default {
    name: 'exportable-table',
    data () {
        return {
            columns: [
                {
                    title: '序号',
                    key: 'rownum',
                    align: 'center',
                    width: 60,
                    render: (h, params) => {
                        return h('div', {}, ((this.pageIndex - 1) * this.pageSize + params.index + 1));
                    }
                }, {title: '主题', key: 'themeid', render: (h, params) => { return h('div', {}, params.row.theme.name); }},
                {title: '奖项', key: 'diplomasid', render: (h, params) => { return h('div', {}, params.row.diplomas.name); }},
                {title: '所属机构', key: 'orgid', render: (h, params) => { return h('div', {}, params.row.organize.orgName); }},
                {
                    title: '奖状生成时间',
                    key: 'createtime',
                    render: (h, params) => { return h('div', {}, getDate.getDate(params.row.createtime, 'time')); }
                },
                {title: '操作',
                    key: 'action',
                    align: 'center',
                    render: (h, params) => {
                        return h('div', [
                            h('Button', {
                                props: {
                                    type: 'primary',
                                    size: 'small'
                                },
                                style: {
                                    marginRight: '5px'
                                },
                                on: {
                                    click: () => {
                                        this.prizeModal = true;
                                        this.imgpath = CONSTANTS.SERVER_HOST + params.row.imgpath;
                                    }
                                }
                            }, '查看奖状'),
                            h('Button', {
                                props: {
                                    type: 'primary',
                                    size: 'small'
                                },
                                style: {
                                    marginRight: '5px'
                                },
                                on: {
                                    click: () => {
                                        this.imgpath = CONSTANTS.SERVER_HOST + params.row.imgpath;
                                        this.prizeDownload(params.row.id);
                                    }
                                }
                            }, '下载')
                        ]);
                    }
                }
            ],
            member: JSON.parse(Cookies.get('member')),
            data: [],
            total: 0,
            pageIndex: 1,
            pageSize: 10,
            prizeModal: false,
            imgpath: '',
            tableHeight: document.documentElement.clientHeight - 100
        };
    },
    mounted () {
        this.loadList();
    },
    methods: {
        loadList () {
            util.ajax.post('/busine/prizes/list', util.toFormData({
                page: this.pageIndex,
                size: this.pageSize,
                memberid: this.member.id
            })).then(res => {
                this.data = res.data.data.list;
                this.total = res.data.data.total;
            });
        },
        prizeDownload (id) {
            // var link = document.createElement('a');
            // link.download = 'my-image-name.jpeg';
            // link.href = this.imgpath;
            // link.click();

            var img = new Image();
            img.setAttribute('crossOrigin', 'Anonymous');
            img.src = this.imgpath;
            let _this = this;
            img.onload = function () {
                _this.downFile(img, 'my-image-name.jpeg');

                util.ajax.post('/busine/prizes/prizeDownload', util.toFormData({
                    id: id
                })).then(res => {
                    if (res.data.message === 'SUCCESS') {
                        _this.$Message.success('下载完成！');
                    }
                });
            };
        },
        close () {
            Cookies.remove('member');
            this.$router.push({
                name: 'loginMember'
            });
        },
        downFile (domImg, filename) {
            var width = domImg.width;
            var height = domImg.height;
            // 创建隐藏的可下载链接
            var eleLink = document.createElement('a');
            eleLink.download = filename;
            eleLink.style.display = 'none';
            // 图片转base64地址
            var canvas = document.createElement('canvas');
            canvas.width = width;
            canvas.height = height;
            var context = canvas.getContext('2d');
            context.drawImage(domImg, 0, 0, width, height);
            eleLink.href = canvas.toDataURL('image/jpeg');
            // 触发点击
            document.body.appendChild(eleLink);
            eleLink.click();
            // 然后移除
            document.body.removeChild(eleLink);
        }
    }
}